package edu.snake.implement.view;


import edu.snake.core.controller.GameEngine;
import edu.snake.implement.ConfigConstant;
import edu.snake.implement.controller.general.GameEngineImpl;
import edu.snake.implement.view.control.ScoreIndicator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.Color;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


public class View extends JFrame {

    private javax.swing.JButton startButton;
    private javax.swing.JButton pauseButton;
    private javax.swing.JButton endButton;
    private javax.swing.JLabel scoreTextLabel;
    private ScoreIndicator scoreCountTextLabel;
    private JPanel jPanel1;
    private ViewArena arenaPanel;
    private GameEngine gameEngine;

    public View(GameEngine gameEngine) throws HeadlessException {
        this.gameEngine = gameEngine;

        setResizable(false);
        setTitle("Snake");
        setLocationRelativeTo(null);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBackground(Color.black);
        setFocusable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        initComponent();
    }

    private void initComponent() {
        jPanel1 = new JPanel();
        scoreTextLabel = new JLabel();
        scoreCountTextLabel = new ScoreIndicator();
        startButton = new JButton();
        pauseButton = new JButton();
        endButton = new JButton();


        startButton.setText("Start");
        pauseButton.setText("Pause");

        pauseButton.setVisible(false);
        endButton.setVisible(false);
        endButton.setText("End");

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                startButton.setVisible(false);
                pauseButton.setVisible(true);
                endButton.setVisible(true);
                gameEngine.startGame();
            }

        });

        pauseButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                gameEngine.pauseThreads();
            }
        });

        endButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                pauseButton.setVisible(false);
                startButton.setVisible(true);
                endButton.setVisible(false);
                gameEngine.stopThreads();
            }
        });


        scoreTextLabel.setText("Score:");
        scoreCountTextLabel.setText("0000");

        arenaPanel = new ViewArena(
                ConfigConstant.cellWidth * ConfigConstant.cellSizePixel
                , ConfigConstant.cellHeight * ConfigConstant.cellSizePixel
        );




        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(pauseButton
                                                , javax.swing.GroupLayout.PREFERRED_SIZE
                                                , 73
                                                , javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(startButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(endButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreTextLabel)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(scoreCountTextLabel)
                        )
        );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(startButton)
                                .addComponent(endButton)
                                .addComponent(scoreTextLabel)
                                .addComponent(scoreCountTextLabel)
                                .addComponent(pauseButton))
        );

        arenaPanel.setBackground(new Color(51, 51, 51));
        arenaPanel.setFocusable(true);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jPanel1
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
                        .addComponent(arenaPanel
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , javax.swing.GroupLayout.DEFAULT_SIZE
                                , Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                                .addComponent(jPanel1
                                        , javax.swing.GroupLayout.PREFERRED_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(arenaPanel
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , javax.swing.GroupLayout.DEFAULT_SIZE
                                        , Short.MAX_VALUE))
        );

        pack();
    }

    public void setGameEngine(GameEngineImpl gameEngine) {
        this.gameEngine = gameEngine;
    }


    public ViewArena getArenaView() {
        return arenaPanel;
    }

    public void setArenaPanel(ViewArena arenaPanel) {
        this.arenaPanel = arenaPanel;
    }

    public void setScoreCountTextLabel(ScoreIndicator scoreCountTextLabel) {
        this.scoreCountTextLabel = scoreCountTextLabel;
    }

    public ScoreIndicator getScoreCountTextLabel() {
        return scoreCountTextLabel;
    }

    public JButton getEndButton() {
        return endButton;
    }

    public JButton getStartButton() {
        return startButton;
    }

    public JButton getPauseButton() {
        return pauseButton;
    }
}
